// 富文本替换函数
CKEDITOR.replace("editor");

// TODO
let game = JSON.parse(window.localStorage.getItem('game'));
let gid = game['gid'];
let gameName = game['gamename'];
document.getElementById('gameTitle').innerText = gameName;
let gameCover = game['gamecover'];
let comment = [];
let review = [];

// 手风琴折叠函数
layui.use(['element', 'layer'], function(){
  var element = layui.element;
  var layer = layui.layer;

  // 监听折叠
  element.on('collapse(test)', function(data){
    layer.msg('展开状态：' + data.show);
  });
});

var gameDetailVm = new Vue({
  el: '#appGameDetail',
  data: {
    'game': game,
    'gid': gid,
    'gameName': gameName,
    'gameDetail': null,
    'gameCover': gameCover,
    'gameLinkList': null,
    'gameStrategy': null,
  },

  method:{

  },

  mounted:function(){

    axios.post('/game/getGameDetail', this.game).then(function(res){
      gameDetailVm.gameDetail = res.data.data;
    })

    axios.post('/game/getGameLinks', this.game).then(function(res){
      gameDetailVm.gameLinkList = res.data.data;
    })

  }

});

// 游戏测评分页函数
layui.use(['laypage', 'layer'], function(){
  var laypage = layui.laypage
      , layer = layui.layer;

  // 将一段数组分页展示
  // 测试数据
  // 调用游戏测评分页
  laypage.render({
    elem: 'gameReviewPagination'
    , count: review.length
    //自定义首页、尾页、上一页、下一页文本
    , first: '首页'
    , last: '尾页'
    , prev: '<em>上一页</em>'
    , next: '<em>下一页</em>'
    // 调颜色
    , theme: '#1E9FFF'

    , jump: function(obj){
      // 模拟渲染
      document.getElementById('gameReviews').innerHTML = function(){
        var arr = []
            , thisData = review.concat().splice(obj.curr * obj.limit - obj.limit, obj.limit);
        layui.each(thisData, function(index, item){
          arr.push('<li style="float: left">' + item + '</li>');
        });
        return arr.join('');
      }();
    }
  });

  let comment = comment;

  // 评论区分页
  laypage.render({
    elem: 'gameCommentPagination',
    count: comment.length,
    //自定义首页、尾页、上一页、下一页文本
    first: '首页',
    last: '尾页',
    prev: '<em>上一页</em>',
    next: '<em>下一页</em>',
    // 调颜色
    theme: '#1E9FFF',

    jump: function(obj){
      // 模拟渲染
      document.getElementById('gameComments').innerHTML = function(){
        var arr = []
            , thisComment = comment.concat().splice(obj.curr * obj.limit - obj.limit, obj.limit);
        layui.each(thisComment, function(index, item){
          arr.push('<li><div class="layui-card"><div class="layui-row">'+
              '<div class="layui-card-header"><span>'+item+ '</span>' +
              '<span style="float: right">'+'16L'+'&nbsp;&nbsp;&nbsp;&nbsp;<button type="button" class="layui-btn layui-btn-xs">评论</button></span></div></div>' +
              '<div class="layui-card-body" style="border-width: 0 0 1px 0;border-style: solid;border-color: #ececec;">'+ item+'</div></div></li>');
        });
        return arr.join('');
      }();
    }
  });

});
